| Conditions | 16 |
| Total Lines | 111 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like gulp-utils.js ➔ generateContentTasks often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | const utils = require("./utils.js"); |
||
| 172 | function generateContentTasks( |
||
| 173 | dir, |
||
| 174 | extraSources, |
||
| 175 | extensionName, |
||
| 176 | baseTask, |
||
| 177 | destinationWebDir, |
||
| 178 | destinationReleaseDir, |
||
| 179 | zipName |
||
| 180 | ) { |
||
| 181 | // Checks which tasks must be generated. If dirs are omitted, tasks are too |
||
| 182 | const executeWebTasks = destinationWebDir !== ""; |
||
| 183 | const executeReleaseTasks = destinationReleaseDir !== ""; |
||
| 184 | |||
| 185 | // Get out of here if no directories are provided |
||
| 186 | if (!executeWebTasks && !executeReleaseTasks) { |
||
| 187 | return {}; |
||
| 188 | } |
||
| 189 | |||
| 190 | const releaseTasks = ["release-do:" + baseTask]; |
||
| 191 | const copyTasks = ["copy-do:" + baseTask]; |
||
| 192 | const watchTasks = ["watch-main:" + baseTask]; |
||
| 193 | const composerExists = fs.existsSync(dir + "/composer.json"); |
||
| 194 | const sources = [dir + "/**"].concat(extraSources); |
||
| 195 | |||
| 196 | if (composerExists) { |
||
| 197 | task("composer:" + baseTask, function () { |
||
| 198 | return composer({ "working-dir": dir }); |
||
| 199 | }); |
||
| 200 | releaseTasks.unshift("composer:" + baseTask); |
||
| 201 | copyTasks.unshift("composer:" + baseTask); |
||
| 202 | watchTasks.unshift("watch-composer:" + baseTask); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (executeReleaseTasks) { |
||
| 206 | // Release tasks |
||
| 207 | const releaseFunction = function () { |
||
| 208 | const versionNumber = |
||
| 209 | config.useVersions === true |
||
| 210 | ? utils.getManifestVersion( |
||
| 211 | dir + "/" + extensionName + ".xml" |
||
| 212 | ) |
||
| 213 | : ""; |
||
| 214 | const versionName = |
||
| 215 | versionNumber !== "" ? "-v" + versionNumber : ""; |
||
| 216 | return src(sources) |
||
| 217 | .pipe(zip(zipName + versionName + ".zip")) |
||
| 218 | .pipe(dest(config.releaseDir + "/" + destinationReleaseDir)); |
||
| 219 | }; |
||
| 220 | if (composerExists) { |
||
| 221 | task("release-do:" + baseTask, releaseFunction); |
||
| 222 | task("release:" + baseTask, parallel(releaseTasks)); |
||
| 223 | } else { |
||
| 224 | task("release:" + baseTask, releaseFunction); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | if (executeWebTasks) { |
||
| 229 | // Clean task |
||
| 230 | task("clean:" + baseTask, function () { |
||
| 231 | return src(config.wwwDir + "/" + destinationWebDir, { |
||
| 232 | allowEmpty: true, |
||
| 233 | }).pipe( |
||
| 234 | vinylPaths(function (paths) { |
||
| 235 | del.sync(paths, { force: true }); |
||
| 236 | return Promise.resolve(); |
||
| 237 | }) |
||
| 238 | ); |
||
| 239 | }); |
||
| 240 | |||
| 241 | // Copy tasks |
||
| 242 | task( |
||
| 243 | "copy-do:" + baseTask, |
||
| 244 | series("clean:" + baseTask, function () { |
||
| 245 | return src(sources).pipe( |
||
| 246 | dest(config.wwwDir + "/" + destinationWebDir) |
||
| 247 | ); |
||
| 248 | }) |
||
| 249 | ); |
||
| 250 | task("copy:" + baseTask, series(copyTasks)); |
||
| 251 | |||
| 252 | // Watch tasks |
||
| 253 | const watchFunction = function () { |
||
| 254 | return watch( |
||
| 255 | sources, |
||
| 256 | { interval: config.watchInterval }, |
||
| 257 | series("copy-do:" + baseTask) |
||
| 258 | ); |
||
| 259 | }; |
||
| 260 | |||
| 261 | if (composerExists) { |
||
| 262 | task("watch-composer:" + baseTask, function () { |
||
| 263 | return watch( |
||
| 264 | [dir + "/composer.json", dir + "/composer.lock"], |
||
| 265 | { interval: config.watchInterval }, |
||
| 266 | series("composer:" + baseTask) |
||
| 267 | ); |
||
| 268 | }); |
||
| 269 | task("watch-main:" + baseTask, watchFunction); |
||
| 270 | task("watch:" + baseTask, parallel(watchTasks)); |
||
| 271 | } else { |
||
| 272 | task("watch:" + baseTask, watchFunction); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return generateTaskSet( |
||
| 277 | baseTask, |
||
| 278 | null, |
||
| 279 | executeReleaseTasks, |
||
| 280 | executeWebTasks |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 292 |